package org.raymond.icloud.common.core.security.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.raymond.icloud.common.core.model.Principal;
import org.raymond.icloud.common.core.security.Certification;

import java.time.Instant;

@NoArgsConstructor
@Data
public class UsernamepasswordCertification implements Certification, Principal {
    private String certification;
    private Long expire;
    private Long expireAt;

    private Long id;
    private String username;
    private String password;

    public UsernamepasswordCertification(String u, String p){
        username = u;
        password = p;
    }

    @Override
    public void setExpire(Long expire){
        this.expire = expire;
        Long timestamp = Instant.now().getEpochSecond();
        this.expireAt = timestamp+expire;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        UsernamepasswordCertification that = (UsernamepasswordCertification) o;

        return new EqualsBuilder().append(username, that.username).append(password, that.password).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37).append(username).append(password).toHashCode();
    }

    @Override
    public boolean authenticate(Certification anOther) {
        return equals(anOther);
    }
}
